package com.controller.wx;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.DateUtils;
import com.common.StringUtils;
import com.dao.sys.SysUserMapper;
import com.dao.task.TaskMapper;
import com.model.sys.SysUser;

import freemarker.template.utility.StringUtil;

@Controller
public class WXController {

	static final Logger logger = LoggerFactory.getLogger(WXController.class);
	@Autowired
	private TaskMapper taskMapper;

	@RequestMapping("wx/taskList")
	public String ChangePwd(HttpServletRequest request) {
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysuser");
		String puserString = JSON.toJSONString(sysUser);
		request.getSession().setAttribute("display_user", puserString);
		request.getSession().setAttribute("sysuser", sysUser);
		request.getSession().setAttribute("display_userid", sysUser.getUserid());
		request.setAttribute("currentyear", DateUtils.getCurrentYear());
		request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
		request.setAttribute("currentdate", DateUtils.getCurrentDate());
		HashMap<String, Object> hashtable = new HashMap<>();
		int week = DateUtils.getWeek(new Date());
		String year = DateUtils.getCurrentYear();
		hashtable.put("week", year + week);
		hashtable.put("userid", sysUser.getUserid());
		hashtable.put("username", sysUser.getUsername());
		List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
		request.setAttribute("taskList", list);
		return "wx/taskList";
	}

	@RequestMapping("/wx/taskDelay")
	public String delay(HttpServletRequest request) {
		String proId = request.getParameter("proId");
		String infId = request.getParameter("infId");
		String oldDate = request.getParameter("oldDate");
		request.setAttribute("proID", proId);
		request.setAttribute("infId", infId);
		request.setAttribute("oldDate", oldDate);
//		Map<String, String> hashMap = new HashMap<>();
//		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysuser");
//		HashMap<String, Object> hashtable = new HashMap<>();
//		int week = DateUtils.getWeek(new Date());
//		String year = DateUtils.getCurrentYear();
//		hashtable.put("week", year + week);
//		hashtable.put("userid", sysUser.getUserid());
//		hashtable.put("username", sysUser.getUsername());
//		hashtable.put("proId", proId);
//		hashtable.put("mobile", "0");
//		List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
//		if (list.size() > 0) {
//			request.setAttribute("map1", list.get(0));
//		} else {
//			request.setAttribute("map1", hashMap);
//		}
		return "wx/taskDelay";
	}

	@RequestMapping("/wx/taskAdd")
	public String taskDefault(HttpServletRequest request) {
		String item;
		item = request.getParameter("item");
		item = item.substring(1, item.length() - 1);
		Map<String, String> hashMap = StringUtils.getStringToMap(item);
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysuser");
		String proId = hashMap.get("PRO_ID");
		HashMap<String, Object> hashtable = new HashMap<>();
		int week = DateUtils.getWeek(new Date());
		String year = DateUtils.getCurrentYear();
		hashtable.put("week", year + week);
		hashtable.put("userid", sysUser.getUserid());
		hashtable.put("username", sysUser.getUsername());
		hashtable.put("proId", proId);
		hashtable.put("mobile", "1");
		List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
		if (list.size() > 0) {
			request.setAttribute("map1", list.get(0));
		} else {
			request.setAttribute("map1", hashMap);
		}
		return "wx/taskAdd";
	}
}
